import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 首页排序联系人
showSortContactBottomSheet(
  BuildContext context,
  String sortContact,
) async {
  String _currentSelectCode = sortContact;
  await showDefaultBottomSheet(
    context,
    child: SortContactBottomSheet(
      value: sortContact,
      selectCallBack: (data) {
        _currentSelectCode = data;
      },
    ),
    title: S.current.sort_contact,
    isScrollControlled: true,
  );
  return _currentSelectCode;
}

class SortContactBottomSheet extends StatefulWidget {
  final String value;
  final ValueChanged<String> selectCallBack;

  const SortContactBottomSheet({
    Key key,
    this.value,
    this.selectCallBack,
  }) : super(key: key);

  @override
  _SortContactBottomSheetState createState() => _SortContactBottomSheetState();
}

class _SortContactBottomSheetState extends State<SortContactBottomSheet> {
  List<String> sortContactList = [
    S.current.alphabetical,
    S.current.recently_transacted,
    S.current.transaction_amount,
  ];

  String currentSelect;

  bool get isShowCustom => currentSelect == sortContactList.last;

  @override
  void initState() {
    super.initState();
    currentSelect = widget.value;
  }

  void onChangeSalesTrend(String value) {
    if (currentSelect == value) return;
    setState(() {
      currentSelect = value;
    });
    widget.selectCallBack?.call(value);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: getDp(17),
        ),
        _buildTop(S.current.sort_by),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.only(
            left: getDp(10),
            right: getDp(10),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: sortContactList
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeSalesTrend(e),
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                        horizontal: getDp(34),
                      ),
                      child: NormalRadio(
                        title: e,
                        isSelect: currentSelect == e,
                      ),
                    ))
                .toList(),
          ),
        ),
      ],
    );
  }

  Widget _buildTop(String title) => Container(
        height: getDp(32),
        color: aceWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: getDp(24)),
        alignment: Alignment.centerLeft,
        child: Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 22 / 16,
          ),
        ),
      );
}
